package student.aistar.tvTask;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.data.redis.core.ListOperations;
import org.springframework.data.redis.core.RedisTemplate;
import org.springframework.data.redis.core.ValueOperations;
import org.springframework.scheduling.annotation.Scheduled;
import org.springframework.stereotype.Component;
import student.aistar.service.ITVSeckillService;

import java.util.List;
import java.util.Map;

/**
 * 本类用来演示:
 *
 * @author: qinFeng
 * @date: 2021/10/11 21:11
 */
@Component
public class TaskSeckill {
    @Autowired
    private ITVSeckillService seckillService;
    @Autowired
    private RedisTemplate redisTemplate;

    /**
     * 每隔两秒执行一次
     * 秒杀活动尚未开始时, 把需要秒杀的商品放到redis中
     */
    @Scheduled(cron = "0/2 * * * * *" )
    public void notYetStarted(){

        List<Map<String, Object>> seckillProductList = seckillService.beforeActive();
        //
        if(seckillProductList != null && seckillProductList.size() > 0){
//            System.out.println("秒杀还未开始");
            //表示一个秒杀产品的对应的个数(后面的数字之和代表个数): 电视大师  1 1 1 1 1 1 1 1 1 1 1 1
            ListOperations secProduct = redisTemplate.opsForList();
            //表示一个秒杀产品的状态
            ValueOperations secStatus = redisTemplate.opsForValue();
            for (Map<String, Object> stringObjectMap : seckillProductList) {
                long id = (long) stringObjectMap.get("id");
                Integer num = (Integer) stringObjectMap.get("num");

                //第一次进来秒杀的时候, 加载进redis的数据
                if(secProduct.size("sk_" + id) == 0){
                    //设置每个产品的状态, 还未开始0, 进行中1, 已经结束3
                    secStatus.set("st_" + id, "0");
                    for (int i = 0; i < num; i++){
                        secProduct.leftPush("sk_" + id, id);
                    }
                }

            }
        }else{
//            System.out.println("加载失败");
        }
    }

    /**
     * 当秒杀商品到达秒杀范围时间时, redis对商品状态设置为1
     * 更新mysql状态从0更新1
     */
    @Scheduled(cron = "0/2 * * * * *" )
    public void inProgress(){
        ValueOperations secStatus = redisTemplate.opsForValue();
        //加载出正在被秒杀商品的id
        List<Long> ids = seckillService.findKillId();
        if(ids != null && ids.size() > 0){
//            System.out.println("秒杀活动已经开始");
            //更新状态
            for (Long id : ids) {
                //更新redis
                secStatus.set("st_" + id, "1");
            }
        }

        //更新数据库 [0] -> [1]
        seckillService.updateState();
    }

    @Scheduled(cron = "0/2 * * * * *" )
    public void spikeOver(){
        //加载出正在被秒杀商品的秒杀id
        List<Long> ids = seckillService.findEndId();
        if(ids != null && ids.size() > 0){
//            System.out.println("秒杀结束了");
            for (Long id : ids) {
                redisTemplate.delete("st_" + id);
                redisTemplate.delete("sk_" + id);
            }
        }

        //更新mysql
        //[1] -> [2]
        seckillService.endActive();
    }
}
